using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DailyTransaction
{
    public partial class ChooseDataForm : Form
    {
        private DAO_Rcpt dao = new DAO_Rcpt();
        public string comp_code = "";
        public string comp_name = "";
        public ChooseDataForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (lstData.CurrentRow != null && lstData.CurrentRow.Cells["COMP_CODE"] != null)
            {
                comp_code = lstData.CurrentRow.Cells["COMP_CODE"].Value.ToString();
                comp_name = lstData.CurrentRow.Cells["COMP_NAME"].Value.ToString();
            }
            else
            {
                comp_code = "";
                comp_name = "";
            }
            this.Close();
        }

        private void ChooseDataForm_Load(object sender, EventArgs e)
        {
            cboFields.SelectedIndex = 0;
            cboOps.SelectedIndex = 6;
            txtFilter.Text = "";
            lstData.DataSource = bindingSource1;
            lblRslt.Text = "Record existing in the database:";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cboFields.SelectedItem.ToString().Trim() == "")
            {
                MessageBox.Show("Please choose an item in fields list!");
                cboFields.Focus();
                return;
            }
            if (cboOps.SelectedItem.ToString().Trim() == "")
            {
                MessageBox.Show("Please choose an item in operations list!");
                cboOps.Focus();
                return;
            }
            if (txtFilter.Text.Trim() == "")
            {
                MessageBox.Show("Please input value to filter!");
                txtFilter.SelectAll();
                return;
            }
            string sql = "select * from company where ";
            string field = cboFields.SelectedItem.ToString();
            sql += " " + field;
            string oper = cboOps.SelectedItem.ToString();
            if (oper == "like")
            {
                sql += " " + oper + " '%";
                sql += txtFilter.Text.Trim() + "%'";
            }
            else
            {
                sql += " " + oper + " '" + txtFilter.Text.Trim() + "'";                
            }
            sql += " order by comp_code";

            DataTable tbl = dao.executeQuery(sql);
            if (tbl != null && tbl.Rows.Count > 0)
            {
                lblRslt.Text = "Record existing in the database:" + tbl.Rows.Count.ToString() + " record(s) found.";    
            }
            else
                lblRslt.Text = "Record existing in the database: 0 record(s) found.";    
            bindingSource1.DataSource = tbl;                
            bindingSource1.ResetBindings(false);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            comp_code = "";
            comp_name = "";
            this.Close();
        }

              
    }
}